<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model as ThinkModel;
use think\Db;
//银行列表模型
class RechargeTemplate extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_VIPER_RECHARGE_TEMPLATE__';
    protected $name = 'qt_viper_recharge_template';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public static function  createByTemplates($arr,$store_id,$viper_type_id,$user_id)
    {
        foreach ($arr as $item)
        {
            if(empty($item['integral'])){
                $item['integral'] = 0;
            }
            if(empty($item['amount'])){
                $item['amount'] = 0;
            }

            if(empty($item['type'])){
                $item['type'] = '';
            }

            if(empty($item['moth'])){
                $item['moth'] = '';
            }
            if(empty($item['give_amount'])){
                $item['give_amount'] = 0;
            }
            if(empty($item['give_amount'])){
                $item['rate'] = 0;
            }
            $template = self::create([
                'amount' => $item['amount'],
                'rate' => $item['rate'],
                'integral' => $item['integral'],
                'type' => $item['type'],
                'moth' => $item['moth'],
                'give_amount' => $item['give_amount'],
                'store_id' => $store_id,
                'name' => '充值：'.$item['amount'] . ' 赠送：'. $item['give_amount'],
                'viper_type_id' =>$viper_type_id,
            ]);
            if(!empty($item['data'])){
                $insert = [];
                foreach ($item['data'] as $k=>$v){
                    $insert[$k] = [
                        'user_id'=>$user_id,
                        'viper_type_id'=>$viper_type_id,
                        'template_id'=>$template->id,
                        'store_id'=>$store_id,
                        'amount'=>$v['amount'],
                        'times'=>$v['times'],
                        'create_time'=>time(),
                    ];
                }
                Db::name('qt_viper_recharge_template_return')->insertAll($insert);
            }
        }
    }

    public static function getTemplateOfStore($store_id,$viper_type_id)
    {
        $list = self::where(['store_id'=>$store_id,'viper_type_id'=>$viper_type_id])
            ->field('id,name,amount,rate,integral,type,give_amount,moth')
            ->select();
        foreach ($list as $key=>$value){
            $list[$key]['data'] = Db::name('qt_viper_recharge_template_return')
                ->field('times,amount')
                ->where('viper_type_id',$viper_type_id)
                ->where('template_id',$value['id'])
                ->select();
        }
        return $list;
    }

    /**
     * 更新充值模板
     * @param $arr
     * @param $store_id
     * @param $viper_type_id
     * @param $user_id
     */
    public static function  updateByTemplates($arr,$store_id,$viper_type_id,$user_id)
    {
        foreach ($arr as $item)
        {
            if(empty($item['integral'])){
                $item['integral'] = 0;
            }
            if(empty($item['amount'])){
                $item['amount'] = 0;
            }

            if(empty($item['type'])){
                $item['type'] = '';
            }

            if(empty($item['moth'])){
                $item['moth'] = '';
            }
            if(empty($item['give_amount'])){
                $item['give_amount'] = 0;
            }
            if(empty($item['give_amount'])){
                $item['rate'] = 0;
            }
            if(isset($item['id']) && !empty($item['id'])){
                self::where(['id'=>$item['id']])->update([
                    'id'=>$item['id'],
                    'amount' => $item['amount'],
                    'rate' => $item['rate'],
                    'integral' => $item['integral'],
                    'type' => $item['type'],
                    'moth' => $item['moth'],
                    'give_amount' => $item['give_amount'],
                    'store_id' => $store_id,
                    'name' => '充值：'.$item['amount'] . ' 赠送：'. $item['give_amount'],
                    'viper_type_id' =>$viper_type_id,
                ]);
                $templateId = $item['id'];
            }else{
                $template = self::create([
                    'amount' => $item['amount'],
                    'rate' => $item['rate'],
                    'integral' => $item['integral'],
                    'type' => $item['type'],
                    'moth' => $item['moth'],
                    'give_amount' => $item['give_amount'],
                    'store_id' => $store_id,
                    'name' => '充值：'.$item['amount'] . ' 赠送：'. $item['give_amount'],
                    'viper_type_id' =>$viper_type_id,
                ]);
                $templateId = $template->id;
            }

            if(!empty($item['data'])){
                $insert = [];
                foreach ($item['data'] as $k=>$v){
                    $insert[$k] = [
                        'user_id'=>$user_id,
                        'viper_type_id'=>$viper_type_id,
                        'template_id'=>$templateId,
                        'store_id'=>$store_id,
                        'amount'=>$v['amount'],
                        'times'=>$v['times'],
                        'create_time'=>time(),
                    ];
                }
                Db::name('qt_viper_recharge_template_return')->insertAll($insert);
            }
        }
    }


}